﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BUS.DAL
{
    public class KhachHangDAL
    {
        internal static int Insert(KhachHangBUS khachHangBUS)
        {
            var db = DataAccess.Database;
            var customer = new KhachHang
            {
                TenDangNhap = khachHangBUS.TenDangNhap,
                MatKhau = khachHangBUS.MatKhau.EncryptMD5(),
                HoTen = khachHangBUS.HoTen,
                SoDienThoai = khachHangBUS.SoDienThoai,
                DiaChi = khachHangBUS.DiaChi,
                CMND = khachHangBUS.CMND,
                Email = khachHangBUS.Email,
                CauHoi = khachHangBUS.CauHoi,
                TraLoi = khachHangBUS.TraLoi
            };

            db.KhachHangs.InsertOnSubmit(customer);
            db.SubmitChanges();
            return customer.ID;
        }



        internal static void Delete(DatHangBUS datHangBUS)
        {
            var db = DataAccess.Database;
            var kh = db.KhachHangs.SingleOrDefault<KhachHang>(p => p.ID == datHangBUS.KhachHang.ID);
            if (kh != null) db.KhachHangs.DeleteOnSubmit(kh);
            db.SubmitChanges();
        }

        internal static bool CheckLogin(KhachHangBUS khachHangBUS)
        {
            var db = new ShopDataContext();

            var kh =
                db.KhachHangs.FirstOrDefault(
                    k => k.TenDangNhap == khachHangBUS.TenDangNhap &&
                        k.MatKhau == khachHangBUS.MatKhau);
            if (kh != null)
            {
                khachHangBUS.ID = kh.ID;
                khachHangBUS.HoTen = kh.HoTen;
                return true;
            }
            return false;
        }

        internal static KhachHangBUS GetByUserName(KhachHangBUS khachHangBUS)
        {
            var db = new ShopDataContext();

            var kh = db.KhachHangs.FirstOrDefault(k => k.TenDangNhap.Equals(khachHangBUS.TenDangNhap));
            if (kh == null)
                return null;
            var exsiting = new KhachHangBUS
                               {
                                   ID = kh.ID,
                                   TenDangNhap = kh.TenDangNhap,
                                   HoTen = kh.HoTen,
                                   SoDienThoai = kh.SoDienThoai,
                                   DiaChi = kh.DiaChi,
                                   CMND = kh.CMND,
                                   Email = kh.Email,
                                   CauHoi = kh.CauHoi,
                                   TraLoi = kh.TraLoi
                               };
            return exsiting;
        }

        internal static void ChangePass(KhachHangBUS khachHangBUS)
        {
            var db = new ShopDataContext();

            var kh = db.KhachHangs.FirstOrDefault(k => k.TenDangNhap == khachHangBUS.TenDangNhap);

            if (kh == null) return;
            kh.MatKhau = khachHangBUS.MatKhau;

            db.SubmitChanges();
        }


        public static TaiKhoan GetMoney(string strAccountNumber)
        {
            var db = DataAccess.Database;
            var money = db.TaiKhoans.FirstOrDefault(p => p.SoTaiKhoan == strAccountNumber);
            return money;
        }

        public static void UpdateMoneyAccount(TaiKhoan taiKhoan)
        {
            var db = DataAccess.Database;
            var tk = db.TaiKhoans.FirstOrDefault<TaiKhoan>(p => p.SoTaiKhoan == taiKhoan.SoTaiKhoan);
            if (tk == null) return;
            tk.SoTien = taiKhoan.SoTien;
            db.SubmitChanges();
        }
        #region d
        internal static List<KhachHangBUS> GetAll()
        {
            var khachhangs = from kh in DataAccess.Database.KhachHangs
                             select new KhachHangBUS()
                             {
                                 ID = kh.ID,
                                 TenDangNhap = kh.TenDangNhap,
                                 MatKhau = kh.MatKhau,
                                 SoDienThoai = kh.SoDienThoai,
                                 CauHoi = kh.CauHoi,
                                 CMND = kh.CMND,
                                 DiaChi = kh.DiaChi,
                                 Email = kh.Email,
                                 HoTen = kh.HoTen,
                                 TraLoi = kh.TraLoi,
                                 GhiChu=kh.GhiChu,
                             };
            return khachhangs.ToList<KhachHangBUS>();
        }
        internal static KhachHangBUS GetByID(int ID)
        {
            var khachhangs = from kh in DataAccess.Database.KhachHangs
                             where kh.ID==ID
                             select new KhachHangBUS()
                             {
                                 ID = kh.ID,
                                 TenDangNhap = kh.TenDangNhap,
                                 MatKhau = kh.MatKhau,
                                 SoDienThoai = kh.SoDienThoai,
                                 CauHoi = kh.CauHoi,
                                 CMND = kh.CMND,
                                 DiaChi = kh.DiaChi,
                                 Email = kh.Email,
                                 HoTen = kh.HoTen,
                                 TraLoi = kh.TraLoi,
                                 GhiChu = kh.GhiChu,
                             };
            return khachhangs.FirstOrDefault<KhachHangBUS>();
        }
        internal static void UpDateGhiChu(KhachHangBUS khachHangBUS)
        {
            ShopDataContext db = new ShopDataContext();
            KhachHang kh = db.KhachHangs.Single(p => p.ID == khachHangBUS.ID);
            kh.GhiChu = khachHangBUS.GhiChu;
           db.SubmitChanges();
        }
        internal static void Delete(KhachHangBUS khachHangBUS)
        {
            ShopDataContext db = new ShopDataContext();
            KhachHang kh = db.KhachHangs.Single(p => p.ID == khachHangBUS.ID);
            db.KhachHangs.DeleteOnSubmit(kh);
            db.SubmitChanges();
        }
        #endregion





        
    }
}
